package com.six.work.controller;

import com.six.work.entity.Admin;
import com.six.work.services.AdminServices;
import com.six.work.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminServices adminServices;

    @GetMapping("/list")
    public List<Admin> findAllAdmin(){
        List<Admin> list = adminServices.findAllAdmin();
        return list;
    }

    @GetMapping("/del")
    public Integer deleteByID(@RequestParam("admin_id") Integer admin_id){
        return adminServices.DelAdminByID(admin_id);
    }

    @PostMapping("/add")
    public Integer InsertAdmin(@RequestBody Admin admin){
        return adminServices.insertAdmin(admin);
    }

    @GetMapping("/list1")
    public List<Admin> findAdminByName(String adminName) {
        return adminServices.findAdminByName(adminName);
    }

    @GetMapping("/update")
    public Admin getUpdatePage(@RequestParam("admin_id") Integer admin_id){
        return adminServices.SelectAdminByID(admin_id);
    }

    @PostMapping("/update")
    public Integer UpdateAdmin(@RequestBody Admin admin) {
        return adminServices.updateAdmin(admin);
    }

    @PostMapping("/login")
    public Result UserLogin(@RequestBody Admin admin) {
        Result result = new Result();
        Admin ad = adminServices.UserLogin(admin.getAdmin_name());

        if (ad != null) { //stu返回对象不为空则表示，用户名正确
            if (ad.getAdmin_pass().equals(admin.getAdmin_pass())) { //为真则表示，密码正确
                result.setCode(200);
            } else {  //为假则表示，密码错误
                result.setCode(201);
            }
        } else { //stu返回对象为空则表示，用户不正确
            result.setCode(202);
        }
        result.setObject(ad);
        return result;
    }

}
